package com.example.octopus.service;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class WeixinCloudFunctionCaller {
    private static final String ENV = "你的云开发环境ID";
    private static final String FUNCTION_NAME = "你的云函数名称";
    private static final String API_GATEWAY_URL = "https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=";
    public static void callCloudFunction() {
        RestTemplate restTemplate = new RestTemplate();
        String url = API_GATEWAY_URL + "&env=" + ENV + "&name=" + FUNCTION_NAME;
        // 请求体，根据你的云函数定义来构造
        String requestBody = "{\"key\": \"value\"}";
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestBody, String.class);
        // 处理响应
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("云函数调用成功，返回结果：" + response.getBody());
        } else {
            System.out.println("云函数调用失败，状态码：" + response.getStatusCode());
        }
    }
}
